import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CUserHistoryEntity } from '../user/entities/user-history.entity';

@Injectable()
export class HistoryService {
  constructor(
    @InjectRepository(CUserHistoryEntity)
    private readonly userHistoryRepository: Repository<CUserHistoryEntity>,
  ) {}

  async getUserHistory(
    userId: number, 
    page = 1,
    pageSize = 10
  ): Promise<{ items: CUserHistoryEntity[]; meta: any }> {
    console.log(`==== HistoryService: getUserHistory called: userId=${userId}, page=${page}, pageSize=${pageSize}`);
    
    const [items, total] = await this.userHistoryRepository.findAndCount({
      where: { userId },
      order: { createdAt: 'DESC' },
      skip: (page - 1) * pageSize,
      take: pageSize,
    });

    console.log(`==== HistoryService: Found ${items.length} history records for user ${userId}`);

    return {
      items,
      meta: {
        total,
        page,
        pageSize,
      },
    };
  }
} 